using UnityEngine;
using UnityEngine.Rendering;
using UnityEngine.Rendering.RenderGraphModule;

namespace CNGraphi.U6URP
{
    /// <summary>
    /// 吸附效果
    /// <para>作者：CN.Graphi</para>
    /// </summary>
    [VolumeComponentMenu("G Post-processing/Adsorb")]
    public class Adsorb : GPostProcess
    {
        #region 参数
        public Vector3Parameter centerPosition = new Vector3Parameter(new Vector3(0, 0.01f,0), true);
        public MinFloatParameter vaildDistance = new MinFloatParameter(0.1f, 0f, true);
        [Space(10)]
        public ColorParameter clr = new ColorParameter(Color.white, true, true, false, true);
        public Texture2DParameter tex = new Texture2DParameter(null, true);
        public Vector4Parameter tillingAndOffsets = new Vector4Parameter(new Vector4(1.0f, 1.0f, 0.0f, 0.0f), true);
        public BoolParameter supportTransparent = new BoolParameter(true, true);
        [Space(10)]
        public MinFloatParameter radiansOuter = new MinFloatParameter(1.0f, 0.02f, true);
        public MinFloatParameter radiansInner = new MinFloatParameter(0.1f, 0.0f, true);
        public MinFloatParameter innerSmooth = new MinFloatParameter(1f, 0.0f, true);
        public MinFloatParameter outEdgeRange = new MinFloatParameter(0.01f, 0.0f, true);
        public ColorParameter outEdgeClr = new ColorParameter(Color.white, true, true, false, true);
        [Space(10)]
        public Vector2Parameter fadeRange = new Vector2Parameter(new Vector2(2.0f, 5.0f),true);
        public MinFloatParameter fadePow = new MinFloatParameter(5f, 1f, true);
        [Space(10)]
        public ClampedFloatParameter intensity = new ClampedFloatParameter(0.0f, 0.0f, 1.0f, true);
        #endregion


        public override bool IsActive() => mMaterial != null && intensity.value != 0;


        internal override void OnRender(CommandBuffer cmd, PostProcessPassData passData, TextureHandle src, TextureHandle dst)
        {
            if (mMaterial == null) { return; }

            // 输入参数
            mMaterial.SetFloat("_intensity", intensity.value);
            mMaterial.SetColor("_clr", clr.value);
            mMaterial.SetVector("_centerPosition", centerPosition.value);
            mMaterial.SetFloat("_radiansOuter", radiansOuter.value);
            mMaterial.SetFloat("_radiansInner", radiansInner.value);
            mMaterial.SetFloat("_innerSmooth", innerSmooth.value);
            mMaterial.SetTexture("_tex", tex.value);
            mMaterial.SetVector("_tillingAndOffsets", tillingAndOffsets.value);
            mMaterial.SetFloat("_outEdgeRange", outEdgeRange.value);
            mMaterial.SetColor("_outEdgeClr", outEdgeClr.value);
            mMaterial.SetVector("_fadeRange", fadeRange.value);
            mMaterial.SetFloat("_fadePow", fadePow.value);
            mMaterial.SetFloat("_vaildDistance", vaildDistance.value);
            mMaterial.SetFloat("_supportTransparent", supportTransparent.value ? 1 : 0);

            // 绘制
            Draw(cmd, src, dst, 0);
        }

        internal override void OnTextureCreate(RenderGraph renderGraph, ContextContainer frameData)
        {
        }

        internal override void OnTextureUseCommit(IUnsafeRenderGraphBuilder builder)
        {
        }

        protected override void OnDispose()
        {
        }

    }
}